package top.z.orm.handle.osql.core;

import top.z.orm.core.osql.annotation.SysValue;
import top.z.orm.handle.util.ObjectCreatorFactory;

import java.lang.reflect.Field;

public class SysValueProcessor {
    public static <T> void process(T obj) {
        Field[] f = obj.getClass().getDeclaredFields();
        String  value;
        String  key;
        String  proValue;
        for (Field field : f) {
            if (field.isAnnotationPresent(SysValue.class)) {
                SysValue sysValue = field.getAnnotation(SysValue.class);
                value = sysValue.value();
                if ("".equals(value.trim())) {

                } else {
                    value = value.trim();
                    if (value.startsWith("${") && value.endsWith("}")) { //  ${bee.properties.key}
                        key = value.substring(2, value.length() - 1);
                        proValue = BeeProp.getBeeProp(key);
                        if (proValue == null)
                            continue;
                        else
                            value = proValue;
                    }
                    try {
                        Class<?> c = field.getType();
                        field.setAccessible(true);
                        field.set(obj, ObjectCreatorFactory.create(value, c));
                    } catch (IllegalAccessException e) {
                        throw ExceptionHelper.convert(e);
                    }
                }
            }
        }
    }
}